<template>
  <div class="permission-example">
    <h2>权限控制使用示例</h2>
    
    <!-- 示例1: 使用权限按钮组件 -->
    <div class="example-section">
      <h3>1. 使用 PermissionButton 组件</h3>
      <div class="button-group">
        <!-- 基础用法 -->
        <PermissionButton
          permission="user@User@store"
          type="primary"
          icon="plus"
          @click="handleCreate"
        >
          新增用户
        </PermissionButton>
        
        <!-- 不同样式 -->
        <PermissionButton
          permission="user@User@update"
          type="success"
          size="small"
          icon="edit"
          @click="handleEdit"
        >
          编辑
        </PermissionButton>
        
        <!-- 危险操作 -->
        <PermissionButton
          permission="user@User@destroy"
          type="danger"
          size="small"
          icon="trash"
          @click="handleDelete"
        >
          删除
        </PermissionButton>
      </div>
    </div>
    
    <!-- 示例2: 使用权限检查函数 -->
    <div class="example-section">
      <h3>2. 使用 usePermission Composable</h3>
      
      <!-- 单个权限检查 -->
      <div v-if="hasPermission('user@User@index')">
        <p>✅ 您有查看用户列表的权限</p>
      </div>
      
      <!-- 多权限检查 (必须全部拥有) -->
      <div v-if="hasAllPermissions(['user@User@store', 'user@User@update'])">
        <p>✅ 您拥有新增和编辑用户的权限</p>
      </div>
      
      <!-- 多权限检查 (拥有任一即可) -->
      <div v-if="hasAnyPermission(['user@User@update', 'user@User@destroy'])">
        <p>✅ 您可以管理用户（编辑或删除）</p>
      </div>
      
      <!-- 复杂条件判断 -->
      <div class="complex-permission">
        <h4>复杂权限控制示例：</h4>
        <div v-if="canManageUsers">
          <p>🎯 管理员控制面板</p>
          <PermissionButton
            permission="system@Config@update"
            type="warning"
            @click="handleSystemConfig"
          >
            系统配置
          </PermissionButton>
        </div>
      </div>
    </div>
    
    <!-- 示例3: 权限信息展示 -->
    <div class="example-section">
      <h3>3. 权限信息展示</h3>
      <div class="permission-info">
        <p><strong>用户权限总数：</strong>{{ userPermissionCount }}</p>
        <p><strong>当前页面权限：</strong></p>
        <ul>
          <li v-for="permission in currentPagePermissions" :key="permission">
            {{ permission }} - 
            <span :class="hasPermission(permission) ? 'text-green-600' : 'text-red-600'">
              {{ hasPermission(permission) ? '✅ 有权限' : '❌ 无权限' }}
            </span>
          </li>
        </ul>
      </div>
    </div>
    
    <!-- 示例4: 表格操作按钮 -->
    <div class="example-section">
      <h3>4. 表格操作按钮示例</h3>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <PermissionButton
              permission="user@User@update"
              type="primary"
              size="small"
              text
              icon="edit"
              @click="handleEditUser(scope.row)"
            >
              编辑
            </PermissionButton>
            
            <PermissionButton
              permission="user@User@destroy"
              type="danger"
              size="small"
              text
              icon="trash"
              class="ml-2"
              @click="handleDeleteUser(scope.row)"
            >
              删除
            </PermissionButton>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { usePermission } from '@/composables/usePermission'
import PermissionButton from '@/components/PermissionButton.vue'

// 使用权限 Composable
const {
  hasPermission,
  hasAllPermissions,
  hasAnyPermission,
  userHasPermissions,
  debugPermissions
} = usePermission()

// 示例数据
const tableData = ref([
  { id: 1, name: '张三', email: 'zhangsan@example.com' },
  { id: 2, name: '李四', email: 'lisi@example.com' }
])

// 当前页面需要的权限
const currentPagePermissions = [
  'user@User@index',
  'user@User@store', 
  'user@User@update',
  'user@User@destroy',
  'system@Config@update'
]

// 计算属性
const userPermissionCount = computed(() => {
  return userHasPermissions.value?.length || 0
})

const canManageUsers = computed(() => {
  return hasAllPermissions([
    'user@User@index',
    'user@User@store',
    'user@User@update'
  ])
})

// 事件处理函数
const handleCreate = () => {
  console.log('执行新增用户操作')
}

const handleEdit = () => {
  console.log('执行编辑操作')
}

const handleDelete = () => {
  console.log('执行删除操作')
}

const handleSystemConfig = () => {
  console.log('打开系统配置')
}

const handleEditUser = (user: any) => {
  console.log('编辑用户:', user)
}

const handleDeleteUser = (user: any) => {
  console.log('删除用户:', user)
}

// 页面初始化
onMounted(() => {
  // 打印权限调试信息
  debugPermissions('权限使用示例页面', currentPagePermissions)
})
</script>

<style scoped>
.permission-example {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.example-section {
  margin-bottom: 30px;
  padding: 20px;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  background-color: #fafafa;
}

.button-group {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.permission-info {
  background-color: #f8f9fa;
  padding: 15px;
  border-radius: 6px;
  border-left: 4px solid #409eff;
}

.complex-permission {
  margin-top: 15px;
  padding: 15px;
  background-color: #fff;
  border-radius: 6px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.text-green-600 {
  color: #059669;
  font-weight: 500;
}

.text-red-600 {
  color: #dc2626;
  font-weight: 500;
}

h2 {
  color: #303133;
  border-bottom: 2px solid #409eff;
  padding-bottom: 10px;
  margin-bottom: 20px;
}

h3 {
  color: #606266;
  margin-bottom: 15px;
}

h4 {
  color: #909399;
  margin-bottom: 10px;
}

ul {
  margin: 10px 0;
  padding-left: 20px;
}

li {
  margin: 8px 0;
  line-height: 1.5;
}
</style> 